package com.bchun.gamepk.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.bchun.gamepk.model.Page;
import com.bchun.gamepk.service.IBaseService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * <pre>
 * FutureSoftware
 * 
 * File: BaseAction.java
 * 
 * Copyright (C) By PeterFeng at 2013
 * 
 * Description: 
 * 
 * Notes:
 * 
 * Revision History
 * Date          Name         Desc
 * 2013年9月20日         PeterFeng    Initial design
 * 
 * </pre>
 */
public abstract class BaseAction<T> extends ActionSupport {

	private final Log log = LogFactory.getLog(getClass());

	private static final long serialVersionUID = -5835966404480571510L;

	public static String RESULT = "result";
	protected static String LIST = "list";
	protected static String ADD = "add";
	protected static String EDIT = "edit";
	protected static String VIEW = "view";

	protected Class<T> cls;

	protected Page<T> page;
	protected T model;

	protected String resultMessage = "";

	/**
	 * @return
	 */
	abstract protected IBaseService<T> service();

	abstract protected List<?> cbxSelect();

	/**
	 * 棰勮淇℃伅澶勭悊
	 * 
	 * @return
	 */
	public String find() {
		try {
			model = service().getById(cls, getRequest().getParameter("id"));
			return SUCCESS;
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
			return ERROR;
		}
	}

	/**
	 * 鍒楄〃淇℃伅澶勭悊
	 * 
	 * @return
	 */
	public String list() {
		try {
			if (page == null)
				page = new Page<T>();
			page = service().selectByPage(page);
			page.setTotalPage((page.getTotalSize() / page.getPageSize()) + 1);
			return SUCCESS;
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
			return ERROR;
		}
	}

	/**
	 * 璺宠浆娣诲姞淇℃伅椤甸潰
	 * 
	 * @return
	 */
	public String toadd() {
		return SUCCESS;
	}

	/**
	 * 娣诲姞淇℃伅澶勭悊
	 * 
	 * @return
	 */
	public String add() {
		try {
			service().insert(model);
			resultMessage = getText("success");
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
		}
		return SUCCESS;
	}

	/**
	 * 缂栬緫椤甸潰璺宠浆澶勭悊
	 * 
	 * @return
	 */
	public String toedit() {
		try {
			model = service().getById(cls, getRequest().getParameter("id"));
			return SUCCESS;
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
			return ERROR;
		}
	}

	/**
	 * 淇敼淇℃伅澶勭悊
	 * 
	 * @return
	 */
	public String edit() {
		try {
			if (model != null) {
				service().update(model);
				resultMessage = getText("success");
			} else {
				resultMessage = getText("error");
			}
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
		}
		return SUCCESS;
	}

	/**
	 * 鍒犻櫎淇℃伅澶勭悊
	 * 
	 * @return
	 */
	public String delete() {
		try {
			log.info(":::" + cbxSelect());
			if (cbxSelect() != null && cbxSelect().size() > 0) {
				service().deleteById(cbxSelect());
				resultMessage = getText("success");
			} else {
				resultMessage = getText("error");
			}
		} catch (Exception e) {
			log.error("", e);
			resultMessage = e.getLocalizedMessage();
		}
		return SUCCESS;
	}

	public void outJsonString(String str) {
		getResponse().setContentType("text/json;charset=UTF-8");
		outString(str);
	}

	public void outString(String str) {
		try {
			getResponse().setHeader("Cache-Control", "no-cache");
			getResponse().setCharacterEncoding("UTF-8");
			PrintWriter out = getResponse().getWriter();
			out.write(str);
			out.flush();
			out.close();
		} catch (IOException e) {
			log.error("", e);
		}
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	public Page<T> getPage() {
		return page;
	}

	public void setPage(Page<T> page) {
		this.page = page;
	}

	public T getModel() {
		return model;
	}

	public void setModel(T model) {
		this.model = model;
	}

	public String getResultMessage() {
		return resultMessage;
	}

	public void setResultMessage(String resultMessage) {
		this.resultMessage = resultMessage;
	}

}
